package com.github.io.controller;

import com.github.io.annotation.Api;
import com.github.io.entity.SbApiData;
import com.github.io.entity.SbApiResultExplanationData;
import com.github.io.service.ISbApiDataService;
import com.github.io.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/9/13 17:09
 * Description:
 */
@RestController
@Api(value = "Api控制器")
@RequestMapping("/apiData")
public class ApiDataController {

    @Autowired
    private ISbApiDataService dataService;

    @PostMapping("/save")
    public Object save(@RequestBody SbApiData sbApiData) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = dataService.saveApiData(sbApiData);
            if (count > 0) {
                result.put("status", 200);
                result.put("msg", "新增成功");
            } else {
                result.put("status", 400);
                result.put("msg", "新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "新增时异常");
        }
        return result;
    }

    @PutMapping("/update")
    public Object update(@RequestBody SbApiData sbApiData) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = dataService.updateApiData(sbApiData);
            if (count > 0) {
                result.put("status", 200);
                result.put("msg", "修改成功");
            } else {
                result.put("status", 400);
                result.put("msg", "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "修改时异常");
        }
        return result;
    }


    @GetMapping("/{id}")
    public Object getApiInfo(@PathVariable("id") String apiId) {

        Map<String, Object> result = new HashMap<>();
        try {
            SbApiData apiData = dataService.getApiInfo(apiId);
            if (!ObjectUtils.isEmpty(apiData)) {
                result.put("status", 200);
                result.put("msg", "获取成功");
                result.put("data", apiData);
            } else {
                result.put("status", 400);
                result.put("msg", "获取失败，数据id不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "获取Api信息时异常");
        }
        return result;
    }


    @GetMapping("/list")
    public Object list() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<SbApiData> apiDataList = dataService.list();
            result.put("status", 200);
            result.put("msg", "获取成功");
            result.put("data", apiDataList);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "获取列表信息时异常");
        }
        return result;
    }

    @GetMapping("/execute")
    public Object execute(@RequestParam String apiId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Object executeData = dataService.execute(apiId);
            if (!ObjectUtils.isEmpty(executeData)) {
                result.put("status", 200);
                result.put("msg", "获取成功");
                result.put("data", executeData);
            } else {
                result.put("status", 400);
                result.put("msg", "执行异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", 400);
            result.put("msg", "获取列表信息时异常");
        }
        return result;
    }
}
